from django.db.models import Q
from django.shortcuts import render
from rest_framework.filters import SearchFilter
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from doctor.doctorser import MajorSerializer, RankSerializer, DoctorSerializer, DeptSerializer, ConsultationSerializer,ConsultationSer
from doctor.models import MajorModel, RankModel, DoctorModel, DeptModel, ConsultationModel


# Create your views here.


class MajorView(ModelViewSet):
    queryset = MajorModel.objects.all()
    serializer_class = MajorSerializer
    lookup_field = 'id'
    lookup_url_kwarg = 'id'


class RankView(ModelViewSet):
    queryset = RankModel.objects.all()
    serializer_class = RankSerializer
    lookup_field = 'id'
    lookup_url_kwarg = 'id'


class DeptView(ModelViewSet):
    queryset = DeptModel.objects.all()
    serializer_class = DeptSerializer
    lookup_field = 'id'
    lookup_url_kwarg = 'id'


class Pagination(PageNumberPagination):
    page_size_query_param = 'size'
    page_query_param = 'page'


class DoctorView(ListAPIView):
    queryset = DoctorModel.objects.all()
    serializer_class = DoctorSerializer
    pagination_class = Pagination
    filter_backends = [SearchFilter]
    search_fields = ['d_name']

    def get_queryset(self):
        query = Q()
        major = self.request.query_params.get('major')
        if int(major) > 0:
            query &= Q(major=int(major))
        return DoctorModel.objects.filter(query & Q(is_show=True) & Q(is_del=False))


# 获取未展示的医生的数据
class DoctorListView(APIView):
    def get(self, request):
        doctor = DoctorModel.objects.filter(Q(is_show=False) & Q(is_del=False))
        ser = DoctorSerializer(doctor, many=True)
        return Response({"message": "获取成功", "data": ser.data})


# 将医生库医生添加到首页
class UpdDepot(APIView):
    def put(self, request):
        id = request.query_params.get('id')
        doctor = DoctorModel.objects.filter(id=id).first()
        doctor.is_show = True
        doctor.save()
        return Response({"message": "添加成功"})


# 删除首页医生
class DelDoctor(APIView):
    def put(self, request):
        id = request.query_params.get('id')
        doctor = DoctorModel.objects.filter(id=id).first()
        doctor.is_del = True
        doctor.save()
        return Response({"message": "删除成功"})


# 手动添加医生
class AddDoctor(APIView):
    def post(self, request):
        d_name = request.data.get('d_name')
        doctor = DoctorModel.objects.filter(d_name=d_name).first()
        if doctor:
            return Response({"code": 400, "message": "医生已存在,请勿重复添加", })
        ser = DoctorSerializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200, "message": "添加成功", "data": ser.data})
        return Response({"code": 400, "message": "添加失败", "err": ser.errors})


# 修改医生信息
class UpdDoctor(APIView):
    def put(self, request):
        id = request.query_params.get('id')
        doctor = DoctorModel.objects.filter(id=id).first()
        if not doctor:
            return Response({"code": 400, "message": "医生不存在"})
        ser = DoctorSerializer(doctor, data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200, "message": "修改成功", "data": ser.data})
        return Response({"code": 400, "message": "修改失败", "err": ser.errors})


# 远程会诊医生展示
class LongDoctorView(ListAPIView):
    queryset = DoctorModel.objects.all()
    serializer_class = DoctorSerializer
    pagination_class = Pagination
    filter_backends = [SearchFilter]
    search_fields = ['d_name']

    def get_queryset(self):
        query = Q()
        region = self.request.query_params.get('region')
        major = self.request.query_params.get('major')
        rank = self.request.query_params.get('rank')
        if region:
            query &= Q(region__contains=region)
        if major:
            query &= Q(major=int(major))
        if rank:
            query &= Q(rank=int(rank))
        return DoctorModel.objects.filter(query & Q(is_long=True) & Q(is_del=False))


# 会诊
class ConsultationView(APIView):
    def get(self, request):
        user = request.query_params.get('user')
        role = request.query_params.get('role')
        doctor = request.query_params.get('doctor')
        if int(role) == 1:
            long_doctor = ConsultationModel.objects.filter(is_del=False)
            ser = ConsultationSerializer(long_doctor, many=True)
            return Response({"message": "ok", "data": ser.data})
        elif int(role) == 2:
            long_doctor = ConsultationModel.objects.filter(doctor=doctor, is_del=False)
            ser = ConsultationSerializer(long_doctor, many=True)
            return Response({"message": "ok", "data": ser.data})
        elif int(role) == 3:
            long_doctor = ConsultationModel.objects.filter(user=user, is_del=False)
            ser = ConsultationSerializer(long_doctor, many=True)
            return Response({"message": "ok", "data": ser.data})


# 申请会诊
class AddConView(APIView):
    def post(self, request):
        #  时长 状态 user doctor
        print(request.data)
        data = {
            "duration": request.data.get('duration'),
            "status": request.data.get('status'),
            "user": request.data.get('user'),
            "doctor": request.data.get('doctor'),
        }
        ser = ConsultationSer(data=data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200, "message": "申请成功", "data": ser.data})
        else:
            return Response({"code": 400, "message": "申请失败", "err": ser.errors})


# 编辑会诊
class UpdConView(APIView):
    def put(self, request):
        id = request.query_params.get('id')
        con = ConsultationModel.objects.filter(id=id).first()
        if not con:
            return Response({"code": 400, "message": "会诊不存在"})
        ser = ConsultationSerializer(con, data=request.data, partial=True)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200, "message": "修改成功", "data": ser.data})
        return Response({"code": 400, "message": "修改失败", "err": ser.errors})
